//
//  HQHotMoreViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/7/18.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQHotMoreViewController.h"

@interface HQHotMoreViewController ()

@end

@implementation HQHotMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    [self addSearchBar:@"搜索样式名称或厂家"];
    
    self.title = @"更多热点样式";
    
    _dataSource = [[NSMutableArray alloc]init];
    // 下拉刷新
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageNo = 1;
        [self requestData];
    }];
    [_collectionView.mj_header beginRefreshing];
    
    
    //创建瀑布流布局
    XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
    //或者一次性设置
    [waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
    waterfall.delegate = self;
    [_collectionView registerNib:[UINib nibWithNibName:@"HQShopCollCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    _collectionView.collectionViewLayout = waterfall;
    
    [self requestData];
}

//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {
    //根据图片的原始尺寸，及显示宽度，等比例缩放来计算显示高度
    //    XRImage *image =  self.images[indexPath.item];
    
    XRImage *image = [XRImage imageWithImageDic:@{@"img":@"http://img.zcool.cn/community/010f87596f13e6a8012193a363df45.jpg@1280w_1l_2o_100sh.jpg",@"itemIndex":[NSNumber numberWithInteger:indexPath.row]}] ;
    return image.imageH / image.imageW * itemWidth;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HQShopCollCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.imageURL = [NSURL URLWithString:_dataSource[indexPath.row][@"s_img"]];
    cell.itemLabel.text = _dataSource[indexPath.row][@"s_name"];
    return cell;
}

#pragma mark 选中某行触发的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HQHotMoreDetailViewController *hotMoreDetailVc = [[HQHotMoreDetailViewController alloc]init];
    hotMoreDetailVc.dataInfo = _dataSource[indexPath.row];
    [self.navigationController pushViewController:hotMoreDetailVc animated:YES];
}

/**
 搜索框触发的方法
 
 @param searchBar 搜索框
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}

/**
 请求案例接口
 */
-(void)requestData{
    _collectionView.backgroundView = nil;
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam getHotStyleLists:@"10" page:[NSString stringWithFormat:@"%ld",_pageNo]] withMethod:[HQMethod getHotStyleLists] suc:^(NSDictionary *sucData) {
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSArray *tempArr = sucData[@"data"];
            
            if(_pageNo == 1){
                [_dataSource removeAllObjects];
                [_dataSource addObjectsFromArray:tempArr];
                if(_dataSource.count >=10){
                    [self addFooter];
                }else{
                    [_collectionView.mj_footer removeFromSuperview];
                }
                if(_dataSource.count == 0){
                    [self addNoneDataCollView:_collectionView withText:@"列表内容为空"];
                }
                [_collectionView reloadData];
            }else{
                [_dataSource addObjectsFromArray:tempArr];
                [_collectionView reloadData];
            }
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
    }];
}

/**
 添加上拉刷新控件
 */
-(void)addFooter{
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageNo ++;
        [self requestData];
    }];
}

@end
